/*
 * @lc app=leetcode.cn id=739 lang=java
 *
 * [739] 每日温度
 */

// @lc code=start

import java.util.Deque;

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        // 使用单调栈
        int[] res = new int[temperatures.length];
        Deque<Integer> stack = new LinkedList<>();

        for(int i = 0; i < temperatures.length; i++) {
            while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                int preIndex = stack.pop();
                res[preIndex] = i - preIndex;
            }
            stack.push(i);
        }
        return res;
    }
}
// @lc code=end

